#include <stdio.h>
#include "FreeRTOS.h"
#include "app_task.h"
#include "semphr.h"
#include "diode.h"
#include "communicatUart3Rx.h"
#include "communicatUart3Tx.h"
#include "communicatUart1Rx.h"
#include "communicatUart1Tx.h"
#include "uart.h"
#include "dma.h" 
#include "communicatXn297l.h"
#include "baseData.h"
#include "adc.h"
#include "appControl.h"

TaskHandle_t appTaskCreate_Handle = NULL;	
TaskHandle_t ledTaskCreate_Handle = NULL;	 

TaskHandle_t usart1TxTaskCreate_Handle = NULL;
TaskHandle_t usart1RxTaskCreate_Handle = NULL;
TaskHandle_t usart3TxTaskCreate_Handle = NULL;
TaskHandle_t usart3RxTaskCreate_Handle = NULL;
TaskHandle_t vi5300TaskCreate_Handle= NULL;	

TaskHandle_t readAdcTaskCreate_Handle = NULL;	
SemaphoreHandle_t CountSem_Handle = NULL;	


TimerHandle_t           Timer1Timer_Handler;/* 定时器1句柄 */
uint32_t timer1_num = 0;

void taskVi5300(void)
{
	BaseType_t xRerun = pdTRUE;
	while(1)								
	{											
		xRerun = xSemaphoreTake(CountSem_Handle,1000);
		if(pdTRUE == xRerun)						
		{	
			if(vi5300IsExistFlag==1)
			{
				tofControl();		
			}			
			shakeControl();
			xSemaphoreGive(CountSem_Handle);	
		}				
		vTaskDelay(20);
	}
}
void taskReadADC(void)
{
	BaseType_t xRerun = pdTRUE;
	while(1)
	{
		xRerun = xSemaphoreTake(CountSem_Handle,1000);
		if(pdTRUE == xRerun)					
		{ 		
			readAdc();
			xSemaphoreGive(CountSem_Handle);	
		}				
		vTaskDelay(100);
	}
}

void taskUart1Tx(void)
{
	BaseType_t xRerun = pdTRUE;
	while(1)
	{		
		xRerun = xSemaphoreTake(CountSem_Handle,1000);
		if(pdTRUE == xRerun)					
		{ 										
			uart1SendUpdate();				
			xSemaphoreGive(CountSem_Handle);	
		}				
		vTaskDelay(100);
	}
}
void taskUart3Tx(void)
{
	BaseType_t xRerun = pdTRUE;
	while(1)
	{
		xRerun = xSemaphoreTake(CountSem_Handle,1000);
		if(pdTRUE == xRerun)
		{ 
			uart3SendUpdate();
			xSemaphoreGive(CountSem_Handle);	
		}				
		vTaskDelay(100);
	}
}
 
void taskUart1Rx(void)
{
	BaseType_t xRerun = pdTRUE;
	while(1)
	{		
		xRerun = xSemaphoreTake(CountSem_Handle,1000);
		if(pdTRUE == xRerun)					
		{ 										
			uart1RecieveUpdate();				
			xSemaphoreGive(CountSem_Handle);	
		}				
		vTaskDelay(5);
	}
}


void taskUart3Rx(void)
{
	BaseType_t xRerun = pdTRUE;
	while(1)
	{		
		xRerun = xSemaphoreTake(CountSem_Handle,1000);
		if(pdTRUE == xRerun)					
		{ 										
			uart3RecieveUpdate();				
			xSemaphoreGive(CountSem_Handle);	
		}				
		vTaskDelay(5);
	}
}


void taskDiode(void)
{
	BaseType_t xRerun = pdTRUE;
	while (1)
	{
		xRerun = xSemaphoreTake(CountSem_Handle,1000);
		if(pdTRUE == xRerun)
		{ 
			DIODE_TOGGLE();
			xSemaphoreGive(CountSem_Handle);
		}
		vTaskDelay(200);
	}
}	

void Timer1Callback(TimerHandle_t xTimer)
{	
	printf("%d ms\r\n",(++timer1_num)*10);
}	
	
/***
*pc->  <-usart1-usart3->  <-usart1-usart3->  <-usart1-usart3->
*
*
*/
void appTaskCreate(void) 
{	
	BaseType_t xReturn = pdPASS;
	taskENTER_CRITICAL();
				
	CountSem_Handle = xSemaphoreCreateCounting(1,1);
	if(NULL != CountSem_Handle)
	{			
		//printf("\nSemapore Create is not NULL\r\n"); 
	}	
    Timer1Timer_Handler = xTimerCreate((const char*  )"Timer1",(TickType_t)10,(UBaseType_t)pdTRUE,(void*)1,(TimerCallbackFunction_t)Timer1Callback); 
  	xReturn = xTaskCreate((TaskFunction_t)taskUart1Rx	,(const char *)"usart1_rx_task "	,(uint16_t)128,(void*)NULL,(BaseType_t)5,(TaskHandle_t*)&usart1RxTaskCreate_Handle);
//   	xReturn = xTaskCreate((TaskFunction_t)taskUart3Rx	,(const char *)"usart3_rx_task "	,(uint16_t)128,(void*)NULL,(BaseType_t)6,(TaskHandle_t*)&usart3RxTaskCreate_Handle);
  	xReturn = xTaskCreate((TaskFunction_t)taskUart1Tx	,(const char *)"usart1_tx_task "	,(uint16_t)128,(void*)NULL,(BaseType_t)4,(TaskHandle_t*)&usart1TxTaskCreate_Handle);
// 	xReturn = xTaskCreate((TaskFunction_t)taskUart3Tx	,(const char *)"usart3_tx_task "	,(uint16_t)128,(void*)NULL,(BaseType_t)3,(TaskHandle_t*)&usart3TxTaskCreate_Handle);
  	xReturn = xTaskCreate((TaskFunction_t)taskVi5300	,(const char *)"tasktaskVi5300 "	,(uint16_t)128,(void*)NULL,(BaseType_t)2,(TaskHandle_t*)&vi5300TaskCreate_Handle);				
 	xReturn = xTaskCreate((TaskFunction_t)taskReadADC	,(const char *)"read_ADC "			,(uint16_t)128,(void*)NULL,(BaseType_t)1,(TaskHandle_t*)&readAdcTaskCreate_Handle);				
	//xReturn = xTaskCreate((TaskFunction_t)taskDiode	,(const char *)"led_task"			,(uint16_t)128,(void*)NULL,(BaseType_t)1,(TaskHandle_t*)&ledTaskCreate_Handle);
	
	if(pdPASS == xReturn)				
	{									
		//printf("\nCreate Task is ok \r\n");
	}									
	vTaskDelete(appTaskCreate_Handle);	
	taskEXIT_CRITICAL();
}	

